package com.syyo.gulimall.ware.controller;

//import org.springframework.security.access.prepost.PreAuthorize;

import com.syyo.common.utils.ResultUtils;
import com.syyo.common.vo.SkuHasStockVo;
import com.syyo.gulimall.ware.vo.LockStockResultVo;
import com.syyo.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.syyo.gulimall.ware.domain.req.WareSkuReq;
import com.syyo.gulimall.ware.service.WareSkuService;
import com.syyo.common.domain.ResultVo;

import java.util.List;


/**
 * 商品库存
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 10:06:54
 */
@RestController
@RequestMapping("/ware/waresku")
public class WareSkuController {

    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 新增
     */
    @PostMapping
    // @PreAuthorize("@syyo.check('system:waresku:add')")
    public ResultVo add(@RequestBody WareSkuReq req) {

        return ResultUtils.ok(wareSkuService.add(req));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    // @PreAuthorize("@syyo.check('system:waresku:del')")
    public ResultVo del(@PathVariable("id") Integer id) {
        return ResultUtils.ok(wareSkuService.del(id));
    }

    /**
     * 修改
     */
    @PutMapping
    // @PreAuthorize("@syyo.check('system:waresku:edit')")
    public ResultVo edit(@RequestBody WareSkuReq req) {
        return ResultUtils.ok(wareSkuService.edit(req));
    }

    /**
     * 信息
     */
    @GetMapping("/{id}")
    public ResultVo findOne(@PathVariable("id") Integer id) {
        return ResultUtils.ok(wareSkuService.findOne(id));
    }

    /**
     * 列表
     */
    @GetMapping("/list/{pageNum}/{pageSize}")
    public ResultVo findAll(@PathVariable("pageNum") Integer pageNum,
                            @PathVariable("pageSize") Integer pageSize,
                            WareSkuReq req) {
        return ResultUtils.ok(wareSkuService.findAll(pageNum, pageSize, req));
    }

    /**
     * 查询是否有库存
     */
    @GetMapping("/hasStock")
    public List<SkuHasStockVo> findSkuHasStock(@RequestParam("skuIds") List<Long> skuIds) {
        return wareSkuService.findSkuHasStock(skuIds);
    }

    /**
     * 锁定库存
     */
    @PostMapping("/lock/order")
    public ResultVo orderLockStock(@RequestBody WareSkuLockVo vo) {
        return ResultUtils.ok(wareSkuService.orderLockStock(vo));
    }
}
